from django.core.files.storage import FileSystemStorage
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render
from django.db import connection
import requests
import json
import os


def get_all_path(path):  # 传入存储的list
    temp_list = []
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if not os.path.isdir(file_path):
            temp_list.append(file_path)
    return temp_list


def index(request):
    file_list_string = ''
    file_path = os.path.abspath(__file__).split("index.py")[0] + "templates\html_demo\\"
    for ii in get_all_path(file_path):
        ii = ii.split("\\")[-1]
        # print(ii)
        file_list_string = file_list_string + ii.strip() + ","
    print(file_list_string)
    resp = {
        'file_list': file_list_string,
    }
    return render(request, 'index/inde.html', context=resp)


class CustomFileSystemStorage(FileSystemStorage):
    def get_available_name(self, name, max_length=None):
        # Remove the existing file if it exists
        if self.exists(name):
            self.delete(name)
        return name

def save_html_txt(fileID__, html_or_txt , type__):
    if type__ == "txt":
        img_file_name = fileID__ + ".txt"
    if type__ == "html":
        img_file_name = fileID__ + ".html"
    file_path = os.path.abspath(__file__).split("Update_file.py")[0] + "\\templates\\html_demo\\"  #  yxb/templates/html_demo
    file_path = file_path + img_file_name
    print("正在保存 html txt 文件 :  ")
    print(html_or_txt)
    # return False
    # f = open("C:\\Users\\yxb\\Desktop\\123456654.txt", "wb")
    f = open(file_path, "wb")
    f.write(html_or_txt.encode("UTF-8"))
    f.close()
    return 123

def save_img_png(fileID, file_obj):
    img_file_name = fileID + ".png"
    #  yxb/static/img
    file_path = os.path.abspath(__file__).split("Update_file.py")[0] + "\\static\\img\\"
    print(file_path)
    fs = CustomFileSystemStorage(location=file_path)
    save_img = fs.save(img_file_name, file_obj)


@csrf_exempt
def update_file_data(request):
    print("222222222222222")
    # myfile = request.FILES['myfile']  // input 文件类型获取数据
    if request.method == 'POST':
        print("哈哈哈哈哈哈哈哈哈哈哈哈")
        # print(request.body)  # b'user-name=1&user-email=1%40q.com'
        data = {}
        file_id = request.POST.get('file_name_id')
        describe = request.POST.get('describe')
        html_content = request.POST.get('file_content')

        file_id = file_id.split(".")[0]
        print(file_id)
        print(describe)
        print(html_content)
        # return False
        try:
            myfile = request.FILES['myfile']  # input 文件类型获取数据
            print(myfile)  # 输出 文件名称 
            save_img_png(file_id , myfile)           #   保存图像 png
        except:
            print("myFile is null ... ")
            print("myFile is null ... ")
            print("myFile is null ... ")
        # return False
        save_html_txt(file_id, describe , "txt")     #  保存 describe 文本
        save_html_txt(file_id, html_content , "html")     #  保存 html 文本
        data = json.dumps(data)
    return HttpResponse(data, content_type="application/json")
